package cn.scu.ne04jdemo.service.imp;

import cn.scu.ne04jdemo.dao.UserDao;
import cn.scu.ne04jdemo.entitiy.User;
import cn.scu.ne04jdemo.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImp extends ServiceImpl<UserDao, User> implements UserService {

    private final UserDao userDao;

    public UserServiceImp(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public int login(User user) {
        User selectUser = userDao.selectOne(new QueryWrapper<User>().lambda().eq(User::getAccount, user.getAccount()));
        if (selectUser != null && selectUser.getPassword().equals(user.getPassword())){
            return 1;
        }
        return 0;
    }

    @Override
    public int register(User user) {
        User selectUser = userDao.selectOne(new QueryWrapper<User>().lambda().eq(User::getAccount, user.getAccount()));
        // 判断重复用户名
        if (selectUser != null){
            return 0;
        }
        userDao.insert(user);
        return 1;
    }
}
